<template>
  <div id="questionSubmitView">
    <a-form :model="searchParams" layout="inline">
      <!-- 修改编程语言为题目标题 -->
      <a-form-item
        field="questionTitle"
        label="题目标题"
        style="min-width: 240px"
      >
        <a-input
          v-model="searchParams.questionTitle"
          placeholder="请输入题目标题"
          allow-clear
        />
      </a-form-item>

      <!-- 只看我的复选框 -->
      <a-form-item>
        <a-checkbox v-model="showOnlyMe">只看我的</a-checkbox>
      </a-form-item>

      <a-form-item>
        <a-button type="primary" @click="doSubmit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider :size="0" />
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
        showJumper: true,
      }"
      @page-change="onPageChange"
    >
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="toQuestionPage(record)">
            去做题
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch, watchEffect } from "vue";
import {
  QuestionSubmitControllerService,
  QuestionSubmitVO,
  QuestionSubmitVOQueryRequest,
} from "../../../api";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import store from "@/store";

const tableRef = ref();
const dataList = ref<Array<QuestionSubmitVO>>([]);
const total = ref(0);
const searchParams = ref<QuestionSubmitVOQueryRequest>({
  userId: undefined,
  questionTitle: undefined,
  pageSize: 10,
  current: 1,
  sortField: "createTime",
  sortOrder: "desc",
});

const loadData = async () => {
  const res = await QuestionSubmitControllerService.pageQueryQuestionSubmitVo(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data?.records ?? [];
    total.value = res.data?.total ?? 0;
  } else {
    message.error("加载失败，" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(async () => {
  await loadData();
});

const columns = [
  {
    title: "题目",
    dataIndex: "questionTitle",
  },
  {
    title: "判题结果",
    dataIndex: "judgeResult",
  },
  {
    title: "时间消耗",
    dataIndex: "timeUsed",
    // 关键修改点：自定义渲染时间消耗列
    render: ({ record }) => `${record.timeUsed ?? "--"} ms`,
  },
  {
    title: "内存消耗",
    dataIndex: "memoryUsed",
    // 关键修改点：自定义渲染内存消耗列
    render: ({ record }) => `${record.memoryUsed ?? "--"} KB`,
  },
  {
    title: "提交者学号",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
  {
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const router = useRouter();

/**
 * 跳转到做题页面
 * @param questionSubmitVO
 */
const toQuestionPage = (questionSubmitVO: QuestionSubmitVO) => {
  router.push({
    path: `/view/question/${questionSubmitVO.questionId}`,
  });
};

const showOnlyMe = ref(false);
watch(showOnlyMe, (newVal) => {
  let currentUserId = store.state.user.loginUser.id;
  searchParams.value.userId = newVal ? currentUserId : undefined;
});

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
